package nc.bs.impl.qjb;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yonyou.ucf.mdf.util.BIPUtil;
import com.yonyou.ucf.mdf.util.URLConfig;
import nc.bs.itf.qjb.IQjbService;
import nc.vo.pub.BusinessException;

import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;

public class QjbServiceImpl implements IQjbService {
    // 获取登录临时Code的URL
    private final String baseLoginCodeURL = "/iuap-api-gateway/yonbip/yht/getThirdLoginCode";

    @Override
    public String getAccessToken(String type,String url, String appKey, String appSecret) throws BusinessException {
        String tokenURL = this.getJoinURL("", url, URLConfig.ToKenURL);
        return BIPUtil.getAccessToken(tokenURL, appKey, appSecret);
    }
    @Override
    public String getQJBUserIdByPhoneToUserId(String type, String baseURL, String userPhone, String accessToken) throws BusinessException {
        String URL = this.getJoinURL(type, baseURL, URLConfig.BYPhoneToUserURL);
        HashMap<Object, Object> queryParams = new HashMap<>();
        queryParams.put("access_token", accessToken);
        HashMap<Object, Object> bodyParams = new HashMap<>();
        bodyParams.put("searchcode", userPhone);
        String postQueryParamAndBodyParam = BIPUtil.getPostQueryParamAndBodyParam(URL, queryParams, bodyParams);
        if(postQueryParamAndBodyParam == null || "".equals(postQueryParamAndBodyParam)) throw new BusinessException("当前HOPG中人员信息绑定的手机号，暂无获取到友费控对应的人员信息");
        JSONObject jsonObject = JSON.parseObject(postQueryParamAndBodyParam);
        if("200".equals(jsonObject.getString("code"))) {
            JSONArray jsonArray = jsonObject.getJSONObject("data").getJSONArray("content");
            return jsonArray.getJSONObject(0).getString("userId");
        }else{
            throw new BusinessException(postQueryParamAndBodyParam);
        }
    }

    @Override
    public String getThirdLoginCode(String type, String url, String accessToken, String thirdUcId, String userId) throws BusinessException {
        String loginURL = this.getJoinURL(type, url, URLConfig.ThirdLoginCodeURL);
        return BIPUtil.getThirdLoginCode(loginURL, accessToken, thirdUcId, userId);
    }

    @Override
    public String getSignLoginURL(String qycode,String type, String baseURL,String thirdUCId, String redirctUrl, String code) throws Exception {
//        String format = String.format("https://euc.yonyoucloud.com/cas/thirdOauth2CodeLogin?thirdUCId={thirdUcId}&code={code}&service={service}", baseURL, thirdUCId, code);
//        String loginURL = String.format("https://euc.yonyoucloud.com/cas/thirdOauth2CodeLogin?thirdUCId=%s&code=%s&service=%s", baseURL, thirdUCId, code);
        String encodeURL = URLEncoder.encode(redirctUrl, StandardCharsets.UTF_8);
        String loginURL = String.format("%s/cas/thirdOauth2CodeLogin?thirdUCId=%s&code=%s&service=%s", baseURL, thirdUCId, code, encodeURL);
        return loginURL;
    }

    /**
     * 拼接请求的地址
     * @param type
     * @param baseURL
     * @param joinURL
     * @return
     * @throws BusinessException
     */
    protected String getJoinURL(String type,String baseURL, String joinURL) throws BusinessException {
        if("YFK".equals(type)){ // 友费控增加了网关
            return baseURL + URLConfig.GatewayURL+ joinURL;
        } else if ("YHT".equals(type)) {
            return baseURL + URLConfig.GatewayURL+ joinURL;
        }
        return baseURL + joinURL;
    }

}
